{extend name="common@base" /}
{block name="content"}
<style>
  select, input {
    margin: 0 10px;
  }

  .js-config label {
    margin-right: 20px;
  }
</style>
<section class="content">
  <div class="row">
    <div class="col-xs-12">
      <form action="" method="post" class="form">
        <div class="box box-primary">
          <div class="box-header with-border">
            <div class="box-title">以下数据已被管理员设定</div>
          </div>
          <div class="box-body js-config">
            <div class="form-inline">
              {foreach $qType as $type=>$qt}
              <label for="">{$qt}：{$qConfig[$type]['num']}题 &times; {$qConfig[$type]['score']}分 </label>
              {/foreach}
              <label for="">时长：{$config['exam_duration']}分钟 </label>
              <label for="">通过分数：{$config['pass_score']}分 </label>
            </div>
          </div>
        </div>

        <div id="users" class="box box-primary">
          <div class="box-body">
            <button type="button" class="btn btn-default" @click="userSelectClick">选择考生</button>
            <button type="button" class="btn btn-default" @click="paperBuildClick">生成所有考卷</button>
            <span>已选择考生{{userLength}}人</span>
          </div>
          <table class="table" style="word-break:break-all; word-wrap:break-all;">
            <tr>
              <th width="10%">序号</th>
              <th width="15%">姓名</th>
              <th width="65%">考题</th>
              <th width="10%">操作</th>
            </tr>
            <tr v-for="(item,index) in items.users">
              <td>{{index+1}}</td>
              <td>{{item.realname}}<input type="hidden" :name="'form[papers]['+index+'][uid]'" v-model="item.uid"></td>
              <td>{{item.questions}}<input type="hidden" :name="'form[papers]['+index+'][questions]'"
                                           v-model="item.questions"></td>
              <td>
                <button type="button" class="btn btn-default" @click="paperOneClick(index)">
                  {{item.questions ? "切换考卷" : "生成考卷"}}
                </button>
              </td>
            </tr>
          </table>
        </div>
        <div class="box box-deafult">
          <div class="box-footer">
            <button class="btn btn-primary js-ajax-submit" type="submit">保存</button>
            <a href="javascript:history.back()" class="btn btn-default">返回</a>
          </div>
        </div>

      </form>
    </div>
  </div>
</section>
{/block}
{block name="page_script"}
<script>
  require(["vue"], function (Vue) {
    var obj = new Vue({
      el: "#users",
      data: {
        classId: {$classId},
        items: {$papers},
        roomId: "{$row['id']}",
        userLength: 0,
        userSelect: [],
        userSelectLength: 0,
      },
      created: function () {
        var arr = Object.keys(this.items.users);
        this.userLength = arr.length;
      },
      watch: {},
      methods: {
        userSelectClick: function () {
          var that = this;
          var users = that.items.users;
          var uids = [];
          users.forEach(function (v, i) {
            uids[i] = v.uid;
          });
          popup_user_select({users: uids, classId: that.classId, isAll: false}, function (arr) {
            var nowUsers = [];
            var uids = [];
            for (var i = 0; i < that.userLength; i++) {
              var uid = users[i].uid;
              nowUsers[uid] = users[i];
            }
            uids = Object.keys(nowUsers);
            arr.forEach(function (v, i) {
              if (uids.indexOf(v.uid) >= 0) {
                arr[i].questions = nowUsers[v.uid].questions;
              }
            });
            that.items.users = arr;
            that.userLength = arr.length;
          });
        },
        paperBuildClick: function () {
          var that = this;
          var count = that.items.users.length;
          if (count == 0) {
            js_alert("请先选择考生");
            return false;
          }
          $.post("{:url('buildPaper')}", {count: count}, function (result) {
            var data = result.data;
            var users = that.items.users;
            if (result.code == 0) {
              js_alert(result.msg);
              return false;
            }
            users.forEach(function (v, i) {
              v.questions = data[i];
            });
            that.items.users = users;
          });
        },
        paperOneClick: function (index) {
          var that = this;
          $.post("{:url('buildPaper')}", {count: 1}, function (result) {
            var data = result.data;
            var users = that.items.users;
            if (result.code == 0) {
              js_alert(result.msg);
              return false;
            }
            users[index].questions = data[0];
            that.items.users = users;
          });
        }
      },
    });
  });
  require(["layer_config", "ajaxForm"], function (layer) {
    $("#stu-select").on("click", function () {

    })
  });

</script>
{/block}





